const Base = require('../base.js');

module.exports = class extends Base {
  /**
   * @api {GET} /usersearch/?username=:username 用户搜索接口
   * @apiName SearchUser
   * @apiGroup userinfo
   * @apiVersion  1.0.0
   *
   * @apiParam {String} username 用户的用户名或真实姓名 如wangwei,王
   *
   * @apiSuccess (200) {Object[]} user 用户信息
   *
   * @apiSuccess (200) {Number} user.id 用户id
   *
   * @apiSuccess (200) {String} user.name 用户姓名
   *
   * @apiSuccess (200) {String} user.pictureUrl 用户头像地址
   *
   * @apiParamExample  {type} Request-Example:
   * {
   *     "username": "wang"
   * }
   *
   *
   * @apiSuccessExample {json} Success-Response:
   * [{
   *         "id": 27,
   *         "realname": "杜文博",
   *         "pictureUrl": "userImages/TIM图片20171218101900.png"
   * }]
   *
   */
  async getAction() {
    try {
      const userName = this.get('username');
      const user = think.model('user');
      const userData = await user.where({ 'username|realname': ['like', `%${userName}%`] }).field('id, realname, pictureid').select();
      think.logger.debug(userData);
      const { getUserImg } = await think.service('userinfo/user');
      const userDataWithImgUrl = await getUserImg(userData);
      this.body = userDataWithImgUrl;
    } catch (error) {
      this.fail('502');
      think.logger.debug(error);
    }
  }
};
